/**
 * Created by ziyu on 17/3/7.
 */
import { hashHistory } from 'dva/router'
import development from './develop.json'
import production from './production.json'
import localMock from './local.json'
import fatMock from './fat.json'

const env = 'development'

const history = hashHistory
let out = {}

switch (env) {
  case 'development':
    out = { history, ...development, env }
    break
  case 'production':
    out = { history, ...production, env }
    out.origin = getOriginDomainHost(out.origin)
    break
  case 'localMock':
    out = { history, ...localMock, env }
    break
  case 'fatMock':
    out = { history, ...fatMock, env }
    out.origin = getOriginDomainHost(out.origin)
    break
}

function getOriginDomainHost (origin) {
  let domainHost = origin

  let host02 = document.domain
  console.info(host02)
  let protocol = window.location.protocol

  let re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/  // 正则表达式
  if (re.test(host02) || host02 === 'localhost') {
    console.info('test')
  } else {
    console.info('prod')
    let index = host02.indexOf('.')
    if (index >= 0) {
      domainHost = `${protocol}//${host02.substr(0, index)}-api${host02.substr(index)}/`
    }
  }
  console.info(domainHost)
  return domainHost
}

export default out
